#!/bin/bash

# 设备指令下发API - cURL测试脚本
# 使用方法: bash docs/curl_test_examples.sh

# API配置
API_KEY="ak_3d97ebf664db97f3023b336f5fc0ad23"
SECRET_KEY="e61b4d35cf2a29e46e08e36dd075ea585614cd91470719c4411d2b946f677e12"
BASE_URL="http://localhost:8000"

# 生成签名的PHP脚本
generate_signature() {
    local method="$1"
    local path="$2"
    local body="$3"
    local timestamp=$(date +%s)
    
    # 构建待签名字符串
    local string_to_sign="${method}\n${path}\n${timestamp}\n${body}"
    
    # 使用PHP计算签名
    local signature=$(php -r "echo hash_hmac('sha256', '${string_to_sign}', '${SECRET_KEY}');")
    
    echo "${timestamp}|${signature}"
}

echo "=== 设备指令下发API - cURL测试 ==="
echo "API Key: ${API_KEY}"
echo "Base URL: ${BASE_URL}"
echo ""

# 测试1: 设备重启
echo "=== 测试1: 设备重启 ==="
METHOD="POST"
PATH="/api/device/command"
BODY='{"device_id":"1C3426522542","commands":{"cmds":[{"cmd_name":"reboot"}]}}'

# 生成签名
TIMESTAMP_SIGNATURE=$(generate_signature "$METHOD" "$PATH" "$BODY")
TIMESTAMP=$(echo $TIMESTAMP_SIGNATURE | cut -d'|' -f1)
SIGNATURE=$(echo $TIMESTAMP_SIGNATURE | cut -d'|' -f2)

echo "请求体: $BODY"
echo "时间戳: $TIMESTAMP"
echo "签名: $SIGNATURE"
echo ""

curl -X POST "${BASE_URL}${PATH}" \
  -H "Authorization: ApiKey ${API_KEY}" \
  -H "Timestamp: ${TIMESTAMP}" \
  -H "Signature: ${SIGNATURE}" \
  -H "Content-Type: application/json" \
  -H "X-Request-ID: req_$(date +%s)" \
  -d "${BODY}" \
  -w "\n状态码: %{http_code}\n响应时间: %{time_total}s\n\n"

echo "----------------------------------------"

# 测试2: 热点配置
echo "=== 测试2: 热点配置 ==="
BODY='{"device_id":"1C3426522542","commands":{"cmds":[{"cmd_name":"hotspot_modify","cmd_params":{"hotspot_name":"TestWiFi","hotspot_password":"12345678","hotspot_connected_num_max":10,"hotspot_whether_hide":0,"hotspot_band":1}}]}}'

# 生成签名
TIMESTAMP_SIGNATURE=$(generate_signature "$METHOD" "$PATH" "$BODY")
TIMESTAMP=$(echo $TIMESTAMP_SIGNATURE | cut -d'|' -f1)
SIGNATURE=$(echo $TIMESTAMP_SIGNATURE | cut -d'|' -f2)

echo "请求体: $BODY"
echo "时间戳: $TIMESTAMP"
echo "签名: $SIGNATURE"
echo ""

curl -X POST "${BASE_URL}${PATH}" \
  -H "Authorization: ApiKey ${API_KEY}" \
  -H "Timestamp: ${TIMESTAMP}" \
  -H "Signature: ${SIGNATURE}" \
  -H "Content-Type: application/json" \
  -H "X-Request-ID: req_$(date +%s)" \
  -d "${BODY}" \
  -w "\n状态码: %{http_code}\n响应时间: %{time_total}s\n\n"

echo "----------------------------------------"

# 测试3: SIM卡切换
echo "=== 测试3: SIM卡切换 ==="
BODY='{"device_id":"1C3426522542","commands":{"cmds":[{"cmd_name":"sim_switch","cmd_params":{"sim_id":1}}]}}'

# 生成签名
TIMESTAMP_SIGNATURE=$(generate_signature "$METHOD" "$PATH" "$BODY")
TIMESTAMP=$(echo $TIMESTAMP_SIGNATURE | cut -d'|' -f1)
SIGNATURE=$(echo $TIMESTAMP_SIGNATURE | cut -d'|' -f2)

echo "请求体: $BODY"
echo "时间戳: $TIMESTAMP"
echo "签名: $SIGNATURE"
echo ""

curl -X POST "${BASE_URL}${PATH}" \
  -H "Authorization: ApiKey ${API_KEY}" \
  -H "Timestamp: ${TIMESTAMP}" \
  -H "Signature: ${SIGNATURE}" \
  -H "Content-Type: application/json" \
  -H "X-Request-ID: req_$(date +%s)" \
  -d "${BODY}" \
  -w "\n状态码: %{http_code}\n响应时间: %{time_total}s\n\n"

echo "----------------------------------------"

# 测试4: 多个指令组合
echo "=== 测试4: 多个指令组合 ==="
BODY='{"device_id":"1C3426522542","commands":{"cmds":[{"cmd_name":"hotspot_on"},{"cmd_name":"network_speed_limit","cmd_params":{"type":"all","unit":"MB","value":10}},{"cmd_name":"upload_now"}]}}'

# 生成签名
TIMESTAMP_SIGNATURE=$(generate_signature "$METHOD" "$PATH" "$BODY")
TIMESTAMP=$(echo $TIMESTAMP_SIGNATURE | cut -d'|' -f1)
SIGNATURE=$(echo $TIMESTAMP_SIGNATURE | cut -d'|' -f2)

echo "请求体: $BODY"
echo "时间戳: $TIMESTAMP"
echo "签名: $SIGNATURE"
echo ""

curl -X POST "${BASE_URL}${PATH}" \
  -H "Authorization: ApiKey ${API_KEY}" \
  -H "Timestamp: ${TIMESTAMP}" \
  -H "Signature: ${SIGNATURE}" \
  -H "Content-Type: application/json" \
  -H "X-Request-ID: req_$(date +%s)" \
  -d "${BODY}" \
  -w "\n状态码: %{http_code}\n响应时间: %{time_total}s\n\n"

echo "----------------------------------------"

# 测试5: 查看支持的指令（GET请求）
echo "=== 测试5: 查看支持的指令 ==="
METHOD="GET"
PATH="/api/device/commands"
BODY=""

# 生成签名
TIMESTAMP_SIGNATURE=$(generate_signature "$METHOD" "$PATH" "$BODY")
TIMESTAMP=$(echo $TIMESTAMP_SIGNATURE | cut -d'|' -f1)
SIGNATURE=$(echo $TIMESTAMP_SIGNATURE | cut -d'|' -f2)

echo "GET请求: ${BASE_URL}${PATH}"
echo "时间戳: $TIMESTAMP"
echo "签名: $SIGNATURE"
echo ""

curl -X GET "${BASE_URL}${PATH}" \
  -H "Authorization: ApiKey ${API_KEY}" \
  -H "Timestamp: ${TIMESTAMP}" \
  -H "Signature: ${SIGNATURE}" \
  -H "Content-Type: application/json" \
  -w "\n状态码: %{http_code}\n响应时间: %{time_total}s\n\n"

echo "----------------------------------------"

# 测试6: 查看指令示例（GET请求）
echo "=== 测试6: 查看指令示例 ==="
PATH="/api/device/command/examples"

# 生成签名
TIMESTAMP_SIGNATURE=$(generate_signature "$METHOD" "$PATH" "$BODY")
TIMESTAMP=$(echo $TIMESTAMP_SIGNATURE | cut -d'|' -f1)
SIGNATURE=$(echo $TIMESTAMP_SIGNATURE | cut -d'|' -f2)

echo "GET请求: ${BASE_URL}${PATH}"
echo "时间戳: $TIMESTAMP"
echo "签名: $SIGNATURE"
echo ""

curl -X GET "${BASE_URL}${PATH}" \
  -H "Authorization: ApiKey ${API_KEY}" \
  -H "Timestamp: ${TIMESTAMP}" \
  -H "Signature: ${SIGNATURE}" \
  -H "Content-Type: application/json" \
  -w "\n状态码: %{http_code}\n响应时间: %{time_total}s\n\n"

echo "=== 测试完成 ==="
